package com.qf.flash.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qf.flash.R;
import com.qf.flashlib.base.LazyBaseFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 创建日期：2020/2/23 on 19:50
 * 描述:
 * 作者:戴旭东
 */
public abstract class BaseRefreshFragment extends LazyBaseFragment {

    @BindView(R.id.mRecyclerView)
   public RecyclerView mRecyclerView;
    @BindView(R.id.mRefresh)
    public SmartRefreshLayout mRefresh;
    protected int page = 1;
    protected boolean isRefresh;
    @BindView(R.id.fresh_header)
    public ClassicsHeader freshHeader;
    Unbinder unbinder;


    @Override
    protected int setContentView() {
        return R.layout.fragment_refresh;
    }

    @Override
    protected void initView() {
        initRefresh();
        initRecyclerView();
    }

    private void initRefresh() {
        mRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isRefresh = false;
                page++;
                requestData(false, page);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isRefresh = true;
                page = 1;
                requestData(true, page);
            }
        });
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(getLayoutManager());
        mRecyclerView.setAdapter(getRecyclerAdapter());

    }

    protected abstract void requestData(boolean isRefresh, int page);

    public abstract RecyclerView.LayoutManager getLayoutManager();

    public abstract RecyclerView.Adapter getRecyclerAdapter();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
